package IO.stream;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Vector;

public class RWObject {
	Vector<InfoSeri> infoList;
	
	public RWObject() {
		infoList = new Vector<>();
	}

	/* Read info from file into a new list*/
	public void readInfo(String filename){
		ObjectInputStream ois = null;
		try {
			ois = new ObjectInputStream(new FileInputStream(filename));
			infoList = (Vector<InfoSeri>) ois.readObject();
			ois.close();
		} catch (IOException | ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	/* Write all info of list to file*/
	public void writeInfo(String filename){
		ObjectOutputStream oos = null;
		try {
			oos = new ObjectOutputStream(new FileOutputStream(filename));
			oos.writeObject(infoList);
			oos.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/* Add info test*/
	public void addInfo(InfoSeri info){
		infoList.add(info);
	}
	
	
	public void display(){
		for (InfoSeri info : infoList)
			System.out.println(info.toString());
	}
	
	public static void main(String[] args) {
//		RWObject tester = new RWObject();
//		InfoSeri i1 = new InfoSeri("Info1", 111, 1111.1111);
//		InfoSeri i2 = new InfoSeri("Info2", 222, 2222.2222);
//		InfoSeri i3 = new InfoSeri("Info3", 222, 3333.3333);
//		tester.addInfo(i1);
//		tester.addInfo(i2);
//		tester.addInfo(i3);
//		
//		tester.writeInfo("src/IO/stream/file_o.txt");
		
		RWObject tester2 = new RWObject();
		tester2.readInfo("src/IO/stream/file_o.txt");
		
		tester2.display();
		
	}
	
}
